import { remove, getList, save } from '@/api/house/room'
import { getSelects } from '@/api/house/community'
import { getBuildingSelects } from '@/api/house/building'
import { getOptions } from '@/api/option'

export default {
  data() {
    return {
      formVisible: false,
      formTitle: '添加房间',
      isAdd: true,
      form: {
        communityId:'',
        buildingId:'',
        code:'',
        floor:'',
        layout:'',
        direction:'',
        blankFlag:'',
        hasFile:0,
        id: '',
        createBy:'',
        createTime:'',
        buildArea:'',
        useArea:'',
        feeArea:''

      },
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      options:{
        blankFlag:{},
        communityIds:[],
        buildings:[]
      },
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  computed: {

    //表单验证
    rules() {
      return {
        communityId: [
          { required: true, message: '请选择社区', trigger: 'blur' }
        ],
        buildingId: [
          { required: true, message: '请选择大楼', trigger: 'blur' }
        ],
        floor:[
          { required: true, message: '请输入层数', trigger: 'blur' },
          {
            validator (rule, value, callback) {
              if (/(^[1-9]\d*$)/.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正整数'))
              }
            },
            trigger: 'blur'
          }
        ],
        code:[
          { required: true, message: '请输入大楼编号', trigger: 'blur' },
          { min: 1, max: 16, message: '长度在 1 到 16 个字符', trigger: 'blur' }
        ],
        layout:[
          { required: true, message: '请输入户型', trigger: 'blur' },
          { min: 1, max: 64, message: '长度在 1 到 64 个字符', trigger: 'blur' }
        ],
        direction:[
          { required: true, message: '请输入朝向', trigger: 'blur' },
          { min: 1, max: 16, message: '长度在 1 到 64 个字符', trigger: 'blur' }
        ],
        buildArea:[
          { required: true, message: '请输入建筑面积', trigger: 'blur' },
          {
            validator (rule, value, callback) {
              if (/(^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$)/.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正数'))
              }
            },
            trigger: 'blur'
          }
        ],
        useArea:[
          { required: true, message: '请输入使用面积', trigger: 'blur' },
          {
            validator (rule, value, callback) {
              if (/(^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$)/.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正数'))
              }
            },
            trigger: 'blur'
          }
        ],
        feeArea:[
          { required: true, message: '请输入收费面积', trigger: 'blur' },
          {
            validator (rule, value, callback) {
              if (/(^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$)/.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正数'))
              }
            },
            trigger: 'blur'
          }
        ],
        blankFlag: [
          { required: true, message: '请选择是否空房', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.init()
    this.initOptions()
    this.initCommunitys()
  },
  methods: {
    init() {
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })

    },
    initOptions(){
      getOptions('YesOrNo').then(response => {
        this.options.blankFlag = response.data.maps.YesOrNo;
      })
    },
    initCommunitys(){
      getSelects().then(response => {
        this.options.communityIds = response.data;
      })
    },
    getBuildings(){
      this.form.buildingId = '';
      getBuildingSelects(this.form.communityId).then(response => {
        this.options.buildings = response.data;
      })
    },
    search() {
      this.fetchData()
    },
    reset() {
      this.listQuery.id = ''
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClose() {

    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1
      this.fetchData()
    },
    fetchPage(page) {
      this.listQuery.page = page
      this.fetchData()
    },
    changeSize(limit) {
      this.listQuery.limit = limit
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
    },
    resetForm() {
      this.form = {
        communityId:'',
        buildingId:'',
        code:'',
        floor:'',
        layout:'',
        direction:'',
        blankFlag:'',
        hasFile:0,
        id: '',
        createBy:'',
        createTime:'',
        buildArea:'',
        useArea:'',
        feeArea:''
      }
      this.options.buildings=[]
    },
    add() {
      this.resetForm()
      this.formTitle = '添加房间',
      this.formVisible = true
      this.isAdd = true
    },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if(this.form.useArea > this.form.buildArea){
            this.$message({
              message: '使用面积不能大于建筑面积',
              type: 'error'
            })
            return false
          }
          if(this.form.feeArea > this.form.buildArea){
            this.$message({
              message: '收费面积不能大于建筑面积',
              type: 'error'
            })
            return false
          }
          save({
            communityId:this.form.communityId,
            buildingId:this.form.buildingId,
            code:this.form.code,
            floor:this.form.floor,
            layout:this.form.layout,
            direction:this.form.direction,
            blankFlag:this.form.blankFlag,
            hasFile:this.form.hasFile,
            id: this.form.id,
            createBy: this.form.createBy,
            createTime: this.form.createTime,
            buildArea: this.form.buildArea,
            useArea: this.form.useArea,
            feeArea: this.form.feeArea
          }).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
            this.formVisible = false
          })
        } else {
          return false
        }
      })
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning'
      })
      return false
    },
    edit() {
      if (this.checkSel()) {
        this.isAdd = false
        this.form = this.selRow
        this.formTitle = '编辑房间'
        this.formVisible = true
      }
    },
    remove() {
      if (this.checkSel()) {
        var id = this.selRow.id
        this.$confirm(this.$t('common.deleteConfirm'), this.$t('common.tooltip'), {
          confirmButtonText: this.$t('button.submit'),
          cancelButtonText: this.$t('button.cancel'),
          type: 'warning'
        }).then(() => {
          remove(id).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
          }).catch( err=> {
            this.$notify.error({
              title: '错误',
              message: err
            })
          })
        }).catch(() => {
        })
      }
    }

  }
}
